package com.ltv.xiangqiplugin.models;

import java.util.Vector;

public class MoveStack {
	private Vector<String> moves;
	private int index = 0;
	private Vector<MoveStackObserver> observers;
	private static MoveStack instance;
	
	// The moves in vietnam standard
	private Vector<String> vnMoves;
	
	private MoveStack() {
		moves = new Vector<String>();
		observers = new Vector<MoveStackObserver>();
		vnMoves = new Vector<String>();
	}
	
	public static MoveStack getInstance() {
		if (instance == null) {
			instance = new MoveStack();
		}
		
		return instance;
	}
	
	public void addMove(String move) {
		for (int i = 0; i < index; i++) {
			moves.remove(0);
			vnMoves.remove(0);
		}
		index = 0;
		moves.add(0, move);
	}
	
	public void addVnMove(String vnMove) {
		vnMoves.add(0, vnMove);
	}
	
	public void next() {
		if (index > 0) {
			index--;
			notifyObservers(false);
		}
	}
	
	public void last() {
		if (index > 0) {
			index = 0;
			notifyObservers(false);
		}
	}
	
	public void prev() {
		if (index < moves.size() - 1) {
			index++;
			notifyObservers(false);
		}
	}
	
	public void first() {
		if (index < moves.size() - 1) {
			index = moves.size() - 1;
			notifyObservers(false);
		}
	}
	
	public void addObserver(MoveStackObserver observer) {
		observers.add(observer);
	}
	
	public void removeObserver(MoveStackObserver observer) {
		observers.remove(observer);
	}
	
	public void notifyObservers(boolean moveListChanged) {
		for (MoveStackObserver observer : observers) {
			observer.updated(moveListChanged);
		}
	}
	
	public Vector<String> getMoves() {
		return moves;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public void setMoves(Vector<String> moves) {
		this.moves = moves;
	}

	public Vector<String> getVnMoves() {
		return vnMoves;
	}

	public void setVnMoves(Vector<String> vnMoves) {
		this.vnMoves = vnMoves;
	}
	
}
